﻿@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.AcrsStore;
@using SimpleIdServer.IdServer.Website.Stores.AuthMethodsStore;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore
@using SimpleIdServer.IdServer.Website.Stores.IdServerConfigurationStore;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IState<AuthMethodsState> authMethodsStates
@inject NotificationService notificationService
@inject Radzen.DialogService dialogService
@inject IDispatcher dispatcher

<RadzenTemplateForm Submit=@SubmitClientSecret TItem="AddClientSecretForm" Data=@addClientSecret>
    <!-- Secret -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.ClientSecret</RadzenText>
        <RadzenPassword Name="Name" @bind-Value="@addClientSecret.Value" Class="w-100"></RadzenPassword>
        <RadzenRequiredValidator Component="Name" Text="@Global.ClientSecretRequired"></RadzenRequiredValidator>
    </div>
    <!-- Hash algorithm -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.HashAlgorithm</RadzenText>
        <RadzenDropDown @bind-Value="@addClientSecret.Alg"
                Data="@algorithms"
                TextProperty="Text"
                ValueProperty="Value"
                Class="w-100"
                TValue="HashAlgs" />
    </div>
    <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(isAdding ? Global.Adding : Global.Add)" Disabled="@(isAdding)" />
</RadzenTemplateForm>

@code {
    List<HashAlgItem> algorithms = new List<HashAlgItem>
    {
        new HashAlgItem { Value = HashAlgs.PLAINTEXT, Text = "PLAINTEXT" },
        new HashAlgItem { Value = HashAlgs.MD5, Text = "MD5" },
        new HashAlgItem { Value = HashAlgs.SHA1, Text = "SHA1" },
        new HashAlgItem { Value = HashAlgs.SHA256, Text = "SHA256" },
        new HashAlgItem { Value = HashAlgs.SHA384, Text = "SHA384" },
        new HashAlgItem { Value = HashAlgs.SHA512, Text = "SHA512" },
    };

    [Parameter]
    public string Id { get; set; }
    bool isAdding { get; set; }
    record AddClientSecretForm
    {
        public string Value { get; set; }
        public HashAlgs Alg { get; set; }
        public DateTime? ExpirationTime { get; set; }
    }

    List<HashAlgDescription> HashAlgDescriptions = new List<HashAlgDescription>();

    AddClientSecretForm addClientSecret = new AddClientSecretForm();

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            SubscribeToAction<AddClientSecretSuccessAction>((act) =>
            {
                dialogService.Close();
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.ClientSecretAdded });
                StateHasChanged();
            });
        }
    }

    void SubmitClientSecret(AddClientSecretForm addClientSecretForm)
    {
        isAdding = true;
        dispatcher.Dispatch(new AddClientSecretAction
        {
            Alg = addClientSecretForm.Alg,
            Id = Id,
            Value = addClientSecretForm.Value
        });
    }

    private class HashAlgDescription
    {
        public string Name { get; set; }
        public HashAlgs Algs { get; set; }
    }

    private  class HashAlgItem
    {
        public HashAlgs Value { get; set; }
        public string Text { get; set; }
    }
}